import Cookies from 'js-cookie' // 浏览器本地存储 操作cookies的存取

const state = {
  sidebar: { // 侧边栏的配置对象
    // 控制侧边栏是否打开
    // Cookies.get('sidebarStatus') 是字符串
    // 字符串'0'会被隐式转换为 true  数字0 隐式转换为false 字符串''为false
    // +'0' 转换为数字0 为false !+'0'为true  !!+'0'为false
    // +'1'转换为数字1 为true !+'1'为false !!+'1'为true
    // 本地'0'和'1'分别要转换成false和true !!+就可以了
    opened: Cookies.get('sidebarStatus') ? !!+Cookies.get('sidebarStatus') : true,
    withoutAnimation: false // 默认有动画
  },
  device: 'desktop'// 设备切换
}

const mutations = {
  TOGGLE_SIDEBAR: state => {
    state.sidebar.opened = !state.sidebar.opened
    state.sidebar.withoutAnimation = false
    if (state.sidebar.opened) {
      Cookies.set('sidebarStatus', 1)
    } else {
      Cookies.set('sidebarStatus', 0)
    }
  },
  CLOSE_SIDEBAR: (state, withoutAnimation) => {
    Cookies.set('sidebarStatus', 0)
    state.sidebar.opened = false
    state.sidebar.withoutAnimation = withoutAnimation
  },
  TOGGLE_DEVICE: (state, device) => {
    state.device = device
  }
}

const actions = {
  toggleSideBar ({ commit }) {
    commit('TOGGLE_SIDEBAR')
  },
  closeSideBar ({ commit }, { withoutAnimation }) {
    commit('CLOSE_SIDEBAR', withoutAnimation)
  },
  toggleDevice ({ commit }, device) {
    commit('TOGGLE_DEVICE', device)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
